﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 标准单词字典
    /// </summary>
    public class StdWordDictService : CrudAppService<StdWordDict, StdWordDictDto, Guid, StdWordDictInput, StdWordDictCreateDto, StdWordDictUpdateDto>
    {
        private IRepository<StdWordDict, Guid> _stdWordDictRepository;

        public StdWordDictService(IRepository<StdWordDict, Guid> stdWordDictRepository) : base(stdWordDictRepository)
        {
            _stdWordDictRepository = stdWordDictRepository;
        }

        /// <summary>
        /// 获取标准单词
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<StdWordDictDto>> GetListAsync(StdWordDictInput input)
        {
            var iQuery = await _stdWordDictRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.StdWordName.Contains(input.Filter) || i.EnMn.Contains(input.Filter) || i.EnAbbr.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<StdWordDict>, List<StdWordDictDto>>(iQuery.ToList());

                return new PagedResultDto<StdWordDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<StdWordDict>, List<StdWordDictDto>>(items.ToList());

                return new PagedResultDto<StdWordDictDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        /// <returns></returns>
        public async Task PostBatchAsync(List<StdWordDictCreateDto> input)
        {
            var stdWordDicts = ObjectMapper.Map<List<StdWordDictCreateDto>, List<StdWordDict>>(input);
            await _stdWordDictRepository.InsertManyAsync(stdWordDicts);
        }
    }
}
